package com.example.for_screen_push;

import android.media.projection.MediaProjection;
import android.util.Log;

import org.java_websocket.WebSocket;
import org.java_websocket.handshake.ClientHandshake;
import org.java_websocket.server.WebSocketServer;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;

public class SocketLive {
    private WebSocket webSocket;
    CodecLiveH264 codecLiveH264;
    public void sendData(byte[] bytes){
        if (webSocket != null && webSocket.isOpen()){
            webSocket.send(bytes);
        }
    }
    public void start(MediaProjection mediaProjection){
        webSocketServer.start();
        codecLiveH264 = new CodecLiveH264(mediaProjection,this);
        codecLiveH264.startLive();
    }
    public void close(){
        try {
            webSocket.close();

            webSocketServer.stop();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    private WebSocketServer webSocketServer= new WebSocketServer(new InetSocketAddress(9007)){

        @Override
        public void onOpen(WebSocket conn, ClientHandshake handshake) {
            SocketLive.this.webSocket = conn;
            Log.e("111","3444");
        }

        @Override
        public void onClose(WebSocket conn, int code, String reason, boolean remote) {

        }

        @Override
        public void onMessage(WebSocket conn, String message) {

        }

        @Override
        public void onError(WebSocket conn, Exception ex) {
            Log.e("111","222");
        }

        @Override
        public void onStart() {
            Log.e("111","333");
        }
    };
}
